/**
 * for edit
 * load selected labticket for input result.
 * @param labAssignmentTicketId
 * @param selectedLabTicketId
 * @return
 */
function renderSelector(patientId, labAssignmentTicketId, selectedLabTicketId) {

	$.ajax ({
			type	: "GET",
			url		: "../LabResult/renderSelectorAjax?set_ajax_view",
			data	: "labAssignmentTicketId=" + labAssignmentTicketId
			+ "&selectedLabTicketId=" + selectedLabTicketId
			+ "&patientId=" + patientId,
			dataType: 'json',
			success : function(data) {
				if (data.error.code == 'OK') {
					$('div#labResultSelection').html(data.result);
	
				} else {
					displayError(data.error.mess);				
				}
			}

	});	
}

/**
 * for view
* load selected labticket for input result.
* @param labAssignmentTicketId
* @param selectedLabTicketId
* @return
*/
function renderSelector4ViewOnly(ticketId, labAssignmentTicketId, selectedLabTicketId) {

	$.ajax ({
			type	: "GET",
			url		: "../LabResult/renderSelector4ViewOnlyAjax?set_ajax_view",
			data	: "labAssignmentTicketId=" + labAssignmentTicketId
			+ "&selectedLabTicketId=" + selectedLabTicketId
			+ "&ticketId=" + ticketId,
			dataType: 'json',
			success : function(data) {
				if (data.error.code == 'OK') {
					$('div#labResultSelection').html(data.result);
	
				} else {
					displayError(data.error.mess);				
				}
			}

	});	
}

/**
 * for edit
* load selected labticket for input result.
* @param labAssignmentTicketId
* @param selectedLabTicketId
* @return
*/
function renderLabTicketSelector(labAssignmentTicketId, selectedLabTicketId) {

	$.ajax ({
			type	: "GET",
			url		: "../LabResult/RenderLabTicketSelectorAjax?set_ajax_view",
			data	: "labAssignmentTicketId=" + labAssignmentTicketId
			+ "&selectedLabTicketId=" + selectedLabTicketId,
			dataType: 'json',
			success : function(data) {
				if (data.error.code == 'OK') {
					$('div#labTicketListSelect').html(data.result);					
				} else {
					displayError(data.error.mess);				
				}
			}

	});	
}
/**
 * FOr view
 * @param labAssignmentTicketId
 * @param selectedLabTicketId
 * @return
 */
function renderLabTicketSelector4View(labAssignmentTicketId, selectedLabTicketId) {

	$.ajax ({
			type	: "GET",
			url		: "../LabResult/RenderLabTicketSelector4ViewAjax?set_ajax_view",
			data	: "labAssignmentTicketId=" + labAssignmentTicketId
			+ "&selectedLabTicketId=" + selectedLabTicketId,
			dataType: 'json',
			success : function(data) {
				if (data.error.code == 'OK') {
					$('div#labTicketListSelect').html(data.result);					
				} else {
					displayError(data.error.mess);				
				}
			}

	});	
}
/**
 * forEdit
 * @param labAssignmentTicketId
 * @param selectedlabTicketId
 * @return
 */
function renderLabInputResult(labAssignmentTicketId, selectedlabTicketId) {

	$.ajax ({
		type	: "GET",
		url		: "../LabResult/RenderLabResultInputAjax?set_ajax_view",
		data	: "labAssignmentTicketId=" + labAssignmentTicketId
		+ "&selectedLabTicketId=" + selectedlabTicketId,		
		dataType: 'json',
		beforeSend : function () {
			$('#labResultLoaderDiv').show();
		
		},
		success : function(data) {
			if (data.error.code == 'OK') {
				$('div#labResultMeta').html(data.result.meta);
				$('div#labResultContent').html(data.result.resultContent);				

			} else {
				displayError(data.error.mess);				
			}
			$('#labResultLoaderDiv').hide();
		}

	});	
	
}

/**
 * For view
 * @param labAssignmentTicketId
 * @param selectedlabTicketId
 * @return
 */
function renderLabResult4View(labAssignmentTicketId, selectedlabTicketId) {

	$.ajax ({
		type	: "GET",
		url		: "../LabResult/RenderLabResult4ViewAjax?set_ajax_view",
		data	: "labAssignmentTicketId=" + labAssignmentTicketId
		+ "&selectedLabTicketId=" + selectedlabTicketId,		
		dataType: 'json',
		success : function(data) {
			if (data.error.code == 'OK') {
				$('div#labResultMeta').html(data.result.meta);
				$('div#labResultContent').html(data.result.resultContent);				

			} else {
				displayError(data.error.mess);				
			}
		}

	});	
	
}

var currentRow = '000';
var toClose = 0;
function resetEdit() {
	currentRow = '000';
	toClose = 0;
}
function editRow(rowId) {
	if (rowId != currentRow) {
		$('#text_' + rowId).hide();
		$('#edit_' + rowId).show();
		
		$('#text_' + currentRow).text($('#edit_' + currentRow + ' input').val());
		$('#edit_' + currentRow).hide();
		$('#text_' + currentRow).show();
		$('#edit_' + rowId + ' input').focus().select();
		toClose = 0;
		currentRow = rowId;
	} else {
		if (toClose == 0) {
			toClose = 1;
		} else {
			currentRow = '000';
			$('#text_' + rowId).text($('#edit_' + rowId + ' input').val());
			$('#edit_' + rowId).hide();
			$('#text_' + rowId).show();
			toClose = 0;
		}
	}
}

function saveLabResult(print) {	
	//prepare meta data
	//prepare result data
	var labTicketId = $('select#labTicketList :selected').val();
	var labAssignmentTicketId = $('select#labAssignmentTicketList :selected').val();
	if (labTicketId == null && $.trim(labTicketId) == '') {
		return;
	}
	if (labAssignmentTicketId == null && $.trim(labAssignmentTicketId) == '') {
		return;
	}
	var resultData = prepareResultData();
	var metaData = prepareMetaData();
	$.ajax ({
		type	: "GET",
		url		: "../LabResult/SaveLabResultAjax?set_ajax_view",
		data	: "selectedLabTicketId=" + labTicketId
				 +"&labAssignmentTicketId=" + labAssignmentTicketId
		+ resultData + metaData,		
		dataType: 'json',
		beforeSend : function () {
			//$('#labResultLoaderDiv').show();
		
		},
		success : function(data) {
			if (data.error.code == 'OK') {
				renderLabTicketSelector(labAssignmentTicketId, labTicketId);
				showMessage('statusMessage', _('Save successful'), 'success');
				if (print == true) {
					printLabResult(labAssignmentTicketId, labTicketId);					
				}
				resetEdit();
			} else {
				displayError(data.error.mess);				
			}
			//$('#labResultLoaderDiv').hide();
		}

	});	

}
function prepareResultData() {
	var resultData = '';
	var resultDataId = '';
	$("input[class='labItemResultInput']").each(function () {
		resultData += '&resultData[]=' + $(this).val();
		resultDataId += '&labResultId[]=' + $(this).attr('labresultid');		
	});
	return (resultDataId + resultData);
}

function prepareMetaData() {
	var metaData = '';
	var technicianId1 = $('select#technicianId1 :selected').val();
	var technicianId2 = $('select#technicianId2 :selected').val();
	var technician1Name = $('select#technicianId1 :selected').html();
	var technician2Name = $('select#technicianId2 :selected').html();	
	var place = $('#place').val();
	var machine = $('#machine').val();	
	var metaData  = '&' + 'technicianId1=' + technicianId1
						+ '&technicianId2=' + technicianId2
						+ '&place=' + place
						+ '&machine=' + machine
	
	return metaData;
}


function printLabResult(labAssignmentTicketId, selectedlabTicketId) {
	
	var url='../LabResult/PrintLabResultPage?set_print_view&labAssignmentTicketId=' 
		+ labAssignmentTicketId + '&selectedLabTicketId=' + selectedlabTicketId;
	window.open(url, '', 'scrollbars=yes, menubar=no, width=800, height=600, resizable=yes, toolbar=no, location=no, status=no');

}